﻿Imports Suprema
Imports DevComponents.DotNetBar

Module BIOMETRIA
    Friend m_ScannerManager As UFScannerManager
    Friend m_Scanner As UFScanner
    Friend m_Matcher As UFMatcher
    Friend m_Database As UFDatabase
    Friend m_strError As String
    Friend m_Serial As Integer
    Friend m_UserID As String
    Friend m_FingerIndex As Integer
    Friend m_Template1 As Byte()
    Friend m_Template1Size As Integer
    Friend m_Template2 As Byte()
    Friend m_Template2Size As Integer
    Friend m_Memo As String
    '
    Friend Const MAX_USERID_SIZE As Integer = 50
    Friend Const MAX_TEMPLATE_SIZE As Integer = 1024
    Friend Const MAX_MEMO_SIZE As Integer = 100
    '
    Friend Const DATABASE_COL_SERIAL As Integer = 0
    Friend Const DATABASE_COL_USERID As Integer = 1
    Friend Const DATABASE_COL_FINGERINDEX As Integer = 2
    Friend Const DATABASE_COL_TEMPLATE1 As Integer = 3
    Friend Const DATABASE_COL_TEMPLATE2 As Integer = 4
    Friend Const DATABASE_COL_MEMO As Integer = 5
    Sub GenerarNotificaciones(ByVal Texto As String, ByVal Duracion As Integer, ByVal colores As eToastGlowColor, ByVal FORM As Form)
        Try
            Dim glow As eToastGlowColor = colores
            Dim Poe As eToastPosition
            If FORM.WindowState = FormWindowState.Maximized Then
                Poe = eToastPosition.MiddleCenter
            Else
                Poe = eToastPosition.BottomCenter
            End If

            ToastNotification.Show(FORM, Texto, Nothing, Duracion * 1000, glow, Poe)
        Catch ex As Exception
            If colores = eToastGlowColor.Red Then
                Citra_MensajeSistema(Texto, RequierenAntencionTipos.OcurrioError)
            Else
                Citra_MensajeSistema(Texto, RequierenAntencionTipos.MuestraStatus)
            End If
        End Try



    End Sub
    Sub PASO_UNO(ByVal FORM As Form)
        m_ScannerManager = New UFScannerManager(Form)
        m_Scanner = Nothing
        m_Matcher = Nothing
        m_Database = Nothing

        m_Template1 = New Byte(MAX_TEMPLATE_SIZE) {}
        m_Template2 = New Byte(MAX_TEMPLATE_SIZE) {}
    End Sub
    Function PASO_DOS()
        '==========================================================================='
        ' Initilize scanners
        '==========================================================================='
        Dim ufs_res As UFS_STATUS
        Dim nScannerNumber As Integer


        ufs_res = m_ScannerManager.Init()

        If (ufs_res = UFS_STATUS.OK) Then

            GenerarNotificacion("Se conecto con el scanner correctamente", 3, eToastGlowColor.Blue, frmValidarHuella)
        Else
            UFScanner.GetErrorString(ufs_res, m_strError)
            GenerarNotificacion("Se detecto un problema al intentar conectar con el scanner: " & m_strError, 3, eToastGlowColor.Red, frmValidarHuella)
            Exit Function
        End If

        nScannerNumber = m_ScannerManager.Scanners.Count
        'tbxMessage.AppendText("UFScanner GetScannerNumber: " & nScannerNumber & vbNewLine)

        If (nScannerNumber = 0) Then
            GenerarNotificacion("No se encontro ningún scanner conectado, verifique que este conectado y vuelva a intentar la conexión", 5, eToastGlowColor.Red, frmValidarHuella)
            Exit Function
        Else
            BIOMETRIA_INICIADA = True
        End If

        m_Scanner = m_ScannerManager.Scanners(0)
        '==========================================================================='

        '==========================================================================='
        ' Open database
        '==========================================================================='
        Dim ufd_res As UFD_STATUS

        m_Database = New UFDatabase()

        ' Generate connection string
        Dim DataSource As String
        Dim Connection As String
        '

        Connection = BiometriaConnection
        ' Open database
        ufd_res = m_Database.Open(Connection, "", "")
        If (ufd_res = UFD_STATUS.OK) Then

        Else
            UFDatabase.GetErrorString(ufd_res, m_strError)
            GenerarNotificacion("Apertura de la base de datos: " & m_strError, 5, eToastGlowColor.Red, frmValidarHuella)
            Exit Function
        End If

        BIOMETRIA_INICIADA = True
        PASO_DOS = True
        '==========================================================================='
        m_Matcher = New UFMatcher()
        '==========================================================================='
    End Function
    Sub Terminar()
        '==========================================================================='
        ' Uninit scanner module
        '==========================================================================='
        Try
            Dim ufs_res As UFS_STATUS

            Windows.Forms.Cursor.Current = Cursors.WaitCursor
            ufs_res = m_ScannerManager.Uninit()

            If (ufs_res = UFS_STATUS.OK) Then
                ' tbxMessage.AppendText("UFScanner Uninit: OK" & vbNewLine)
            Else
                UFScanner.GetErrorString(ufs_res, m_strError)
                'tbxMessage.AppendText("UFScanner Uninit: " & m_strError & vbNewLine)
            End If

            '==========================================================================='

            '==========================================================================='
            ' Close database
            '==========================================================================='

            If (Not Equals(m_Database, Nothing)) Then
                Dim ufd_res As UFD_STATUS

                ' Close database
                ufd_res = m_Database.Close()
                If (ufd_res = UFD_STATUS.OK) Then
                    'tbxMessage.AppendText("UFDatabase Close: OK" & vbNewLine)
                Else
                    UFDatabase.GetErrorString(ufd_res, m_strError)
                    'tbxMessage.AppendText("UFDatabase Close: " & m_strError & vbNewLine)
                End If
            End If
        Catch ex As Exception

        End Try


    End Sub
    Function UpdateDatabaseList(ByVal form As Form) As List(Of Object)

        If (Equals(m_Database, Nothing)) Then
            Exit Function
        End If

        Dim ufd_res As UFD_STATUS
        Dim DataNumber As Integer
        Dim i As Integer

        UpdateDatabaseList = New List(Of Object)


        ufd_res = m_Database.GetDataNumber(DataNumber)
        If (ufd_res = UFD_STATUS.OK) Then
            '  tbxMessage.AppendText("Registros de la base de datos: " & DataNumber & vbNewLine)
        Else
            UFDatabase.GetErrorString(ufd_res, m_strError)
            '  tbxMessage.AppendText("Registros de la base de datos: " & m_strError & vbNewLine)
            Exit Function
        End If

        UpdateDatabaseList.Clear()

        For i = 0 To DataNumber - 1
            ufd_res = m_Database.GetDataByIndex(i, m_Serial, m_UserID, m_FingerIndex, m_Template1, m_Template1Size, m_Template2, m_Template2Size, m_Memo)
            If (ufd_res <> UFD_STATUS.OK) Then
                UFDatabase.GetErrorString(ufd_res, m_strError)
                GenerarNotificacion("La lectura de la base de datos arrojó un error: " & m_strError, 5, eToastGlowColor.Red, Form)
                Exit Function
            End If
            UpdateDatabaseList.Add({m_Serial, m_UserID, BuscarStrDatoCitra("SELECT NOMBRE_COMPLETO FROM EMPLEADOS WHERE CLAVE='" & m_UserID & "'")})
            ' AddRow(m_Serial, m_UserID)
        Next
    End Function

    Public Function ExtractTemplate(ByRef Template As Byte(), ByRef TemplateSize As Integer, ByVal Form As Form, ByVal Pb As PictureBox) As Boolean
        Dim ufs_res As UFS_STATUS
        Dim EnrollQuality As Integer

        m_Scanner.ClearCaptureImageBuffer()
        TemplateSize = 0
        Do
            ufs_res = m_Scanner.CaptureSingleImage()
            If (ufs_res <> UFS_STATUS.OK) Then
                UFScanner.GetErrorString(ufs_res, m_strError)

                GenerarNotificacion("Error al leer la plantilla de puntos: " & m_strError, 5, eToastGlowColor.Red, Form)
                ExtractTemplate = False
                Exit Function
            End If

            DrawCapturedImage(m_Scanner, Pb)

            ufs_res = m_Scanner.Extract(Template, TemplateSize, EnrollQuality)
            If (ufs_res = UFS_STATUS.OK) Then
                'tbxMessage.AppendText("Extracción de imagen: OK" & vbNewLine)
                Exit Do
            Else
                UFScanner.GetErrorString(ufs_res, m_strError)
                GenerarNotificacion("Error de extracción de puntos: " & m_strError, 5, eToastGlowColor.Red, Form)
            End If
        Loop

        ExtractTemplate = True
    End Function

    Private Sub DrawCapturedImage(ByRef Scanner As UFScanner, ByVal pbImageFrame As PictureBox)
        Dim g As Graphics = pbImageFrame.CreateGraphics()
        Dim rect As Rectangle = New Rectangle(0, 0, pbImageFrame.Width, pbImageFrame.Height)
        Dim Resolution As Integer
        Try
            '
            'Scanner.DrawCaptureImageBuffer(g, rect, cbDetectCore.Checked);
            '
            Dim bitmap As Bitmap = Nothing
            Scanner.GetCaptureImageBuffer(bitmap, Resolution)
            pbImageFrame.Image = bitmap
        Finally
            g.Dispose()
        End Try
    End Sub

End Module
